<!--
 * @Author: Shinkai
 * @Date: 2022-05-26 21:50:47
 * @LastEditTime: 2022-06-08 00:03:56
 * @FilePath: /kai_test/src/App.vue
 * What's up:  加油啊 精致的猪猪男孩!
-->

<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <MyHeader @addTodo="addTodo"></MyHeader>

      <MyList :todos="todos"></MyList>

      <MyFooter
        :todos="todos"
        @appSelectAll="appSelectAll"
        @appClearCheckedTodos="appClearCheckedTodos"
      ></MyFooter>
    </div>
  </div>

</template>
<script>
import pubsub from 'pubsub-js'

import MyFooter from './components/MyFooter'
import MyHeader from './components/MyHeader'
import MyList from './components/MyList'



export default {
  name: 'App',
  data () {
    return {
      todos: JSON.parse(localStorage.getItem('todos')) ?? []
    }
  },
  mounted () {
    //事件总线
    this.$bus.$on('updateTodoCheck', this.updateTodoCheck);
    // this.$bus.$on('filterTodos', this.filterTodos);
    //消息订阅与发布
    pubsub.subscribe('filterTodos', this.filterTodos)
    //更新todo属性
    this.$bus.$on('updateTodoIsEdit', this.updateTodoIsEdit)
    //事件总线 更新todo
    this.$bus.$on('updateTodoEdit', this.updateTodoEdit)
  },
  methods: {
    //添加一个todo
    addTodo (todoObj) {
      console.log('app获取到了参数', todoObj)
      this.todos.unshift(todoObj)
    },
    //更新todo check状态
    updateTodoCheck (id) {
      //id一样,改变done值
      console.log(`app组件todos更新执行了${id}`)
      this.todos.forEach(item => (id === item.id) && (item.done = !item.done))
    },
    //更新todoIsEdit状态
    updateTodoIsEdit (todo) {
      this.todos.forEach(item => (todo.id === item.id) && this.$set(todo,'isEdit', true))
      console.log(`app组件todo的编辑状态更新执行了${todo.isEdit}`)

    },
    //过滤已删除todo
    filterTodos (_, id) {
      // 这个没办法第一个参数好蠢.
      // 用下划线就没问题了,也会高亮
      console.log(`App组件接收到了要删除的${id}`)
      this.todos = this.todos.filter(item => item.id !== id)
    },
    //全选or not
    appSelectAll (checked) {
      this.todos.forEach(item => item.done = checked)
    },
    //清除所有选中的todos
    appClearCheckedTodos () {
      this.todos = this.todos.filter(item => item.done !== true)
    },
    //更新todo
    updateTodoEdit (id, name) {
      this.todos.forEach(item => (id === item.id) && (item.name = name))

    }

  },
  watch: {
    todos: {
      deep: true,
      handler (newVal) {
        localStorage.setItem('todos', JSON.stringify(newVal))
      }
    }
  },
  components: { MyFooter, MyHeader, MyList, }
}


</script>
<style>
/*base*/
body {
  background: #fff;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  margin-right: 8px;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}
.btn-edit {
  color: #fff;
  background-color: #499bda;
  border: 1px solid #397aac;
}
.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}
.btn-edit:hover {
  color: #fff;
  background-color: #3676a6;
}

.btn:focus {
  outline: none;
}
</style>